package cn.vcjf.washcar.common.controller;

import cn.vcjf.washcar.api.user.entity.User;
import cn.vcjf.washcar.api.user.service.UserService;
import cn.vcjf.washcar.common.util.JWT;
import cn.vcjf.washcar.common.util.weixin.WechatUtils;
import cn.vcjf.washcar.common.web.TableResult;
import cn.vcjf.washcar.system.service.SysUserService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class IndexController {
	@Resource
	private SysUserService sysUserService;

	@Resource
	private UserService userService;

	@RequestMapping("/")
	@ResponseBody
	public String wxAuth(){
		return "VK5NVgLN7YW0dGg";
	}
	/**
	 * 获取七牛token
	 */
	@RequestMapping("getUpToken")
	@ResponseBody
	public TableResult getUpToken(String fileName) {
		// 调用方法获取七牛token
		return new TableResult((Object) sysUserService.getUpToken(fileName));
	}

	//处理登录
	@RequestMapping(value = "login")
	public @ResponseBody
	TableResult login(HttpServletRequest request, HttpServletResponse response
			,User userInfo) {
		System.out.println(userInfo.toString());
		String openId = "";
		TableResult result = null;
		try {
			JSONObject minAppOpenId = WechatUtils.getMinAppOpenId(request, response);
			openId = minAppOpenId.getString("openid");
			if (StringUtils.isBlank(openId)) {
				String msg = minAppOpenId.getString("msg");
				return TableResult.ErrorResult("认证失败,获取openId失败!" + msg);
			}
			result = userService.getTokenByOpenId(openId, userInfo);
		} catch (IOException e) {
			e.printStackTrace();
			result = TableResult.ErrorResult("认证失败");
		}
		return result;
	}
	/**
	 * 刷新token
	 */
	@RequestMapping("refreshToken")
	@ResponseBody
	public TableResult refreshToken(HttpServletRequest request) {

		String oldToken = request.getHeader("token");
		String loginId = request.getHeader("loginId");
		String checkSign = JWT.checkSign(oldToken, User.class);
		if (JWT.isExpire(checkSign)>0){
			User userT = JWT.unsign(oldToken,User.class);
			if(StringUtils.isBlank(loginId)||!loginId.equals(userT.getUuId())){
				return TableResult.Result("token校验失败");
			}
			//token已过期，返回新token给客户端
			EntityWrapper<User> wrapper = new EntityWrapper<User>();
			wrapper.eq("uuId", loginId);
			User user = userService.selectOne(wrapper);
			//给用户jwt加密生成token
			String token = JWT.sign(user, 60L * 30L * 1000L);

			JSONObject jsonObject = new JSONObject();
			//封装成对象返回给客户端
			jsonObject.put("token", token);
			jsonObject.put("user", user);
			//已绑定
			if (StringUtils.isNotBlank(user.getPhone())) {
				jsonObject.put("isBind", true);
			} else {
				jsonObject.put("isBind", false);
			}

			return TableResult.Result(jsonObject);
		}
		return TableResult.Result("token校验失败");
	}
	/**
	 * @param title 为附件标题
	 * @param mFile 用于接收上传的附件的对象
	 * */
	@RequestMapping("doUpload")
	@ResponseBody
	public TableResult doUpload(
			String title,
			MultipartFile mFile){
		String fileId = userService.uploadObject(title, mFile);
		Map<String,String> data = new HashMap();
		data.put("fileId",fileId);
		return TableResult.Result(data);
	}
}
